-module(edm_utils).
-include_lib("event_model.hrl").
-export([display_result/3,reset_random/0, exp_random/1, one_buffer_id/0]).

display_result(Finish, Lost, FileSuf) ->
  { {FD1,FD2,FD3},{FT1,FT2,FT3} } = calendar:local_time(),
  File = io_lib:format("statistics/"++FileSuf++"~4.4.0w.~2.2.0w.~2.2.0w-~2.2.0w.~2.2.0w.~2.2.0w.txt",[FD1,FD2,FD3,FT1,FT2,FT3]),
  {ok, S} = file:open(File, write),

  L = length(Lost),
  F = length(Finish),
  io:format("Lost:~p, Finish:~p~n",[L,F]),
  io:format(S,"Lost Percent:~n",[]),
  display_lost_results(5000, Lost, S),
  io:format(S,"Loss Delay time:~n", []),
  display_finish_results(5000, Lost, S),
  io:format(S,"Finish Delay time:~n", []),
  display_finish_results(5000, Finish, S),
  display_packets("Finish",Finish,S),
  display_packets("Lost",Lost,S),
  file:close(S).

display_lost_results(Length, Lost, S) ->
  display_lost_results(Length, 0, Lost,0, S).

display_lost_results(Length, NowLength, [], LostSize,S)->
  io:format(S, "Lost Percent(~p)~p~n",[NowLength, LostSize/Length]),
  finish;

display_lost_results(Length, NowLength, [HL|TL], LostSize, S) ->
  #packet{event_number=EN} = HL,
  if
    EN < NowLength ->
      display_lost_results(Length, NowLength, TL, LostSize+1, S);
    true ->
      io:format(S, "Lost Percent(~p)~p~n",[NowLength, LostSize/Length]),
      display_lost_results(Length, NowLength+Length, [HL|TL], 0, S)
  end.

display_finish_results(_, [],_)->
  finish;

display_finish_results(Length, Data,S)->
  if
    length(Data)<Length ->
      PData = Data,
      LData = [];
    true ->
      {PData , LData} = lists:split(Length,Data)
  end,
  io:format(S, "Delay time:~p~n",[delay_mean(PData)]),
  display_finish_results(Length, LData, S).

delay_mean(Packets) ->
  delay_mean(Packets, length(Packets), 0).

delay_mean([], L, T)->
  if
    L=:=0 ->
      {L, 0.0};
    true ->
      {L, T/L}
  end;

delay_mean([H|T], L, Time)->
  #packet{timestamps=Timestamps} = H,
  if
    length(Timestamps)=:=1 ->
      NewTime = Time,
      NewL = L - 1;
    true ->
      NewTime = Time+delay_mean(Timestamps, 0),
      NewL = L
  end,
  delay_mean(T, NewL, NewTime).

delay_mean([], Time)->
  Time;

delay_mean([H|T], Time)->
  #timestamp{atime=ArriveTime, ctime=LeaveTime}=H,
  NewTime = Time+LeaveTime-ArriveTime,
  delay_mean(T, NewTime)
  .

reset_random()->
  {A1,A2,A3}=now(),
  random:seed(A1,A2,A3).

exp_random(Mean) ->
  -Mean*math:log(1-random:uniform()).

display_packets(_,[], _) ->
  finish;

display_packets(Title, [H|T], S) ->
  io:format(S,"~s Packet Details:~n",[Title]),
  #packet{event_number=Num, plength=Length, sources=Sources, timestamps=Timestamps} = H,
  io:format(S,"Packet(~p):Length=~p,Sources=~p,TimestampsLength=~p~n",[Num,Length,Sources,length(Timestamps)]),
  display_packet_timestamps(lists:reverse(Timestamps), S),
  display_packets(Title, T,S).

display_packet_timestamps([], _) ->
  finish;

display_packet_timestamps([H|T], S) ->
  #timestamp{atime=A,wtime=W,ctime=C} = H,
  io:format(S, "A:~p,W:~p,C:~p~n",[A,W,C]),
  display_packet_timestamps(T,S).

one_buffer_id()->
  -100.
